package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.exception.BaseException;
import com.sky.mapper.DishFlavorsMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description ShoppingCartServiceImpl
 * @Author zhuzi
 * @Date 2023/10/01
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    private final ShoppingCartMapper shoppingCartMapper;

    private final SetmealMapper setmealMapper;

    private final DishMapper dishMapper;

    private final DishFlavorsMapper dishFlavorsMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 判断购物车中商品是否存在
        ShoppingCart shoppingCart = shoppingCartMapper.getShoppingCartByShoppingCartDTO(shoppingCartDTO);
        if (shoppingCart != null) {
            // 存在则数量加一
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateShoppingCartNumber(shoppingCart);
            return;
        }
        // 不存在则判断是菜品还是套餐
        shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCart.setNumber(1);
        if (shoppingCartDTO.getSetmealId() != null) {
            shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());
            // 获取套餐信息
            SetmealVO setmeal = setmealMapper.getSetmealById(shoppingCartDTO.getSetmealId());
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setAmount(setmeal.getPrice());
        } else {
            shoppingCart.setDishId(shoppingCartDTO.getDishId());
            // 获取菜品信息
            Dish dish = dishMapper.getDishByDishId(shoppingCartDTO.getDishId());
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
            shoppingCart.setAmount(dish.getPrice());
        }
        log.info("Get shoppingCart: {}", shoppingCart);
        shoppingCartMapper.insertShoppingCart(shoppingCart);
    }

    /**
     * 获取购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        return shoppingCartMapper.getShoppingCartByUserId(BaseContext.getCurrentId());
    }

    /**
     * 清除购物车
     */
    @Override
    public void deleteShoppingCart() {
        shoppingCartMapper.deleteShoppingCartByUserId(BaseContext.getCurrentId());
    }

    /**
     * 减少购物车商品数量
     *
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCartNumber(ShoppingCartDTO shoppingCartDTO) {
        // 判断购物车中商品是否存在
        ShoppingCart shoppingCart = shoppingCartMapper.getShoppingCartByShoppingCartDTO(shoppingCartDTO);
        if (shoppingCart == null) {
            throw new BaseException("购物车中商品不存在");
        }
        // 如果数量唯一则删除这个条目
        if (shoppingCart.getNumber() == 1) {
            shoppingCartMapper.deleteShoppingCartByShoppingCartId(shoppingCart.getId());
            return;
        }
        // 否则数量减一
        shoppingCart.setNumber(shoppingCart.getNumber() - 1);
        shoppingCartMapper.updateShoppingCartNumber(shoppingCart);
    }

}
